﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using Payroll.BusinessLogic;
using Payroll.BusinessLogic.Audit;
using Payroll.BusinessLogic.Configuration.Settings;
using Payroll.BusinessLogic.Directory;
using Payroll.Common.Utils;
using Payroll.BusinessLogic.Infrastructure;

namespace PayrollHR.Web
{
    public partial class BasePayrollAdministrationUserControl : BasePayrollUserControl
    {
        protected void SelectTab(TabContainer tabContainer, string tabId)
        {
            if (tabContainer == null)
                throw new ArgumentNullException("tabContainer");

            if (!String.IsNullOrEmpty(tabId))
            {
                AjaxControlToolkit.TabPanel tab = tabContainer.FindControl(tabId) as AjaxControlToolkit.TabPanel;
                if (tab != null)
                {
                    tabContainer.ActiveTab = tab;
                }
            }
        }

        protected string GetActiveTabId(TabContainer tabContainer)
        {
            if (tabContainer == null)
                throw new ArgumentNullException("tabContainer");

            if (tabContainer.ActiveTab != null)
                return tabContainer.ActiveTab.ID;

            return string.Empty;
        }

        protected void ProcessException(Exception exc)
        {
            ProcessException(exc, true);
        }

        protected void ProcessException(Exception exc, bool showError)
        {
            this.LogService.InsertLog(LogTypeEnum.AdministrationArea, exc.Message, exc);
            if (showError)
            {
                if (this.SettingManager.GetSettingValueBoolean("Display.AdminArea.ShowFullErrors"))
                {
                    ShowError(exc.Message, exc.ToString());
                }
                else
                {
                    ShowError(exc.Message, string.Empty);
                }
            }
        }

        protected void ShowMessage(string message)
        {
            if (this.Page == null)
                return;

            MasterPage masterPage = this.Page.Master;
            if (masterPage == null)
                return;

            BasePayrollAdministrationMasterPage nopAdministrationMasterPage = masterPage as BasePayrollAdministrationMasterPage;
            if (nopAdministrationMasterPage != null)
                nopAdministrationMasterPage.ShowMessage(message);
        }

        protected void ShowError(string message)
        {
            ShowError(message, string.Empty);
        }

        protected void ShowError(string message, string completeMessage)
        {
            if (this.Page == null)
                return;

            MasterPage masterPage = this.Page.Master;
            if (masterPage == null)
                return;

            BasePayrollAdministrationMasterPage nopAdministrationMasterPage = masterPage as BasePayrollAdministrationMasterPage;
            if (nopAdministrationMasterPage != null)
                nopAdministrationMasterPage.ShowError(message, completeMessage);
        }

        protected virtual List<Language> GetLocalizableLanguagesSupported()
        {
            return this.LanguageService.GetAllLanguages(true);
        }

        protected virtual bool HasLocalizableContent
        {
            get
            {
                var languages = GetLocalizableLanguagesSupported();
                return languages.Count > 1;
            }
        }
        /// <summary>
        /// Merges the rows.
        /// </summary>
        /// <param name="gridView">The grid view.</param>
        protected virtual void MergeRows(GridView gridView)
        {
            for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow row = gridView.Rows[rowIndex];
                GridViewRow previousRow = gridView.Rows[rowIndex + 1];

                for (int i = 0; i < 2; i++)
                {
                    if (row.Cells[i].Text == previousRow.Cells[i].Text)
                    {
                        row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
                                               previousRow.Cells[i].RowSpan + 1;
                        previousRow.Cells[i].Visible = false;
                    }
                }
            }
        }
        /// <summary>
        /// Merges the cell.
        /// </summary>
        /// <param name="colIndices">The col indices.</param>
        /// <param name="gridView">The grid view.</param>
        public void MergeCell(int[] colIndices, GridView gridView)
        {
            foreach (int colIndex in colIndices)
            {
                for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
                {
                    GridViewRow row = gridView.Rows[rowIndex];
                    GridViewRow previousRow = gridView.Rows[rowIndex + 1];

                    if (row.Cells[colIndex].Text == previousRow.Cells[colIndex].Text)
                    {
                        row.Cells[colIndex].RowSpan = previousRow.Cells[colIndex].RowSpan < 2 ? 2 : previousRow.Cells[colIndex].RowSpan + 1;
                        previousRow.Cells[colIndex].Visible = false;
                    }
                }
            }
        }
    }
}